namespace AgileDashboard.controls
{
    using System;
    using System.Collections.Specialized;
    using System.Web;
    using System.Web.UI;

    using AgileDashboard.AppCode;

    public partial class TeamIterationGraph : UserControl
    {
        public int GraphHeight;
        public string Team;
        public string Color;
        public string FadeToColor;
        public string IterationSchedule;

        public IDataConnection DataConnection;

        protected int BackgroundHeight;
        protected TeamStatus TeamStatus;
        protected int ScaledDone;

        private IDataConnectionFactory dataConnectionFactory;
        
        public TeamIterationGraph()
        {
        }

        // public TeamIterationGraph(IDataConnectionFactory dataConnectionFactory, NameValueCollection collection)
        public TeamIterationGraph(NameValueCollection collection)
        {
            this.dataConnectionFactory = dataConnectionFactory;
            this.IterationSchedule = collection["IterationSchedule"];
            this.Team = collection["Team"];
            this.GraphHeight = int.Parse(collection["GraphHeight"]);
        }

        #region Overrides of Control

        protected override void Render(HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(this.Color) && !string.IsNullOrEmpty(this.Team))
            {
                this.Color = Util.FuzzyColorPicker(this.Team);
            }

            // If a 'fade to' color is not specified, use a default value
            if (string.IsNullOrEmpty(this.FadeToColor))
            {
                this.FadeToColor = Util.ScaleColor(this.Color, .25);
            }

            // var dao = new TeamStatusDao(this.dataConnectionFactory.CreateDataConnection());
            var dao = new TeamStatusDao(new RestDataConnection());

            this.TeamStatus = dao.GetTeamStatus(this.IterationSchedule, this.Team);

            if (this.TeamStatus.CurrentIterationCommitmentHours > 0)
            {
                double scaleFactor = this.TeamStatus.CurrentIterationCommitmentHoursMinusToDo / this.TeamStatus.CurrentIterationCommitmentHours;
                this.ScaledDone = Convert.ToInt32(Math.Round(this.GraphHeight * scaleFactor));
                this.BackgroundHeight = this.GraphHeight - this.ScaledDone;
            }
            else
            {
                this.BackgroundHeight = this.GraphHeight;
            }

            base.Render(writer);
        }

        #endregion
    }
}